<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;
use Xu42\Qznjw2014\Qznjw2014 as EducationSystem;


class UserController extends BaseController
{

    /**
     * 用户注册(绑定)
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function register( Request $request )
    {
        $username = $request->input( 'username' );
        $password = $request->input( 'password' );
        $mobile   = $request->input( 'mobile' );
        $openid   = $request->input( 'openid' );

        $this->validate( $request, [
            'username' => 'required',
            'password' => 'required'
        ], [
            'required' => '必填内容'
        ] );

        $isValid = self::isValid( $username, $password );

        if ( !$isValid ) {
            return response()->json( [
                'code'    => 404,
                'message' => '学号或密码错误, 请返回重试',
                'datas'   => ['result' => false, 'desc' => '']
            ] );
        }

        $isRegister = self::isRegister( $username );

        if ( $isRegister ) {  // 已注册, 更新注册信息
            DB::table( 'users' )->where( 'id', $isRegister->id )->update( [
                'password'   => base64_encode( $password ),
                'mobile'     => $mobile,
                'openid'     => $openid,
                'updated_at' => date( 'Y-m-d H:i:s' )
            ] );
        } else {  // 未注册, 添加注册信息
            DB::table( 'users' )->insert( [
                'username'   => $username,
                'password'   => base64_encode( $password ),
                'mobile'     => $mobile,
                'openid'     => $openid,
                'created_at' => date( 'Y-m-d H:i:s' )
            ] );
        }

        return response()->json( [
            'code'    => 200,
            'message' => '绑定成功, 请尽情享用',
            'datas'   => ['result' => true, 'desc' => '']
        ] );
    }


    /**
     * 是否已注册
     * @param $username
     * @return bool
     */
    public static function isRegister( $username )
    {
        $user = DB::table( 'users' )->where( 'username', $username )->first();

        return $user ? $user : false;
    }

    /**
     * 是否已注册
     * @param $openid
     * @return bool
     */
    public static function isRegisterByOpenid( $openid )
    {
        $user = DB::table( 'users' )->where( 'openid', $openid )->first();

        return $user ? $user : false;
    }


    /**
     * 学号密码是否正确
     * @param $username     String 学号
     * @param $password     String 密码
     * @return bool
     */
    public static function isValid( $username, $password )
    {
        $educationSystem = new EducationSystem( $username, $password );

        return $educationSystem->isValid();
    }

    /**
     * 由 openid 获取 username
     * @param $openid
     * @return bool
     */
    public static function getUsernameByOpenid( $openid )
    {
        $user = DB::table( 'users' )->where( 'openid', $openid )->first();

        return $user ? $user->username : false;
    }
}
